package com.andybest.search.interceptor;

import javax.servlet.http.HttpSession;

import org.aopalliance.intercept.MethodInvocation;
import org.seasar.framework.aop.interceptors.AbstractInterceptor;
import org.seasar.framework.container.factory.SingletonS2ContainerFactory;

import com.andybest.search.dto.UserInfo;

public class LoginInterceptor extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Object invoke(MethodInvocation invocation) throws Throwable {
		return isLoggedIn() ? invocation.proceed() : "/login";
	}

	private boolean isLoggedIn() {
		HttpSession session = (HttpSession) SingletonS2ContainerFactory
				.getContainer().getExternalContext().getSession();

		UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");

		return (userInfo != null && userInfo.getUserId() != null);
	}
}
